#!/usr/bin/python
# This program does no error checking
#e-NKA automat
import re
import string
import sys
class automat:


    def __init__(self,id1):
        self.id = id1
        self.izraz = ''
        self.stanje_analizatora = ''
        self.akcije = dict()
        self.states = list()
        self.input_symbols = list()
        self.acceptable_states = ['1']
        self.beginning_states = ['0']
        self.transition_functions = dict()

    def parse_adef(self,adef):
##        global states
##        global input_symbols
##        global acceptable_states
##        global beginning_states
##        global transition_functions

        linec = 0
        #print('usao sam')
        for line in adef:
            if line == '---\n':  #znaci da je dosao do kraja tablice prijelaza
                break
            line = line.strip()
            linec += 1

            if linec == 1:
                self.states += re.split(',', line)
            elif linec == 2:
                tmp = re.split(',', line)
                print(tmp)
                for i in tmp:
                    if i == '\\n':
                        self.input_symbols += '\n'
                    else:
                        self.input_symbols += i
                
    #        elif linec == 3:
    #            acceptable_states += string.split(line, ',')
    #        elif linec == 4:
    #            beginning_states += string.split(line, ',')
            else:
                trsplt = re.split('->', line)
                if trsplt[0].count(',') == 2:
                    poz = trsplt[0].find(',')
                    trsplt2 = []
                    trsplt2 += trsplt[0][:poz]
                    trsplt2 += trsplt[0][poz+1:]
                else:
                    trsplt2 = re.split(',',trsplt[0])
                if trsplt2[1] == '\\n':
                    trsplt2[1] = '\n'
                if trsplt2[1] == '\\t':
                    trsplt2[1] = '\t'
                if trsplt2[1] == '\_':
                    trsplt2[1] = ' '
                if (trsplt2[0], trsplt2[1]) in self.transition_functions:
                    self.transition_functions[(trsplt2[0], trsplt2[1])] += trsplt[1:]
                else:
                    self.transition_functions[(trsplt2[0], trsplt2[1])] = trsplt[1:]

    def add_epsilon (self,curr_states):
        new_states = curr_states[:]
        new = list()

        for state in curr_states:
            if (state, '$') in self.transition_functions.keys():
                new += self.transition_functions[(state, '$')]

        for state in new:
            if state not in curr_states:
                new_states.append(state)

        if curr_states == new_states:
            return new_states
        else:
            return self.add_epsilon(new_states)

    def check_line (self,line):
##        global beginning_states
    #      if (line == '\n'):
  #          line = line.strip()
        vrati = [0,0]
        curr_states = self.beginning_states[:]

        curr_states = self.add_epsilon(curr_states)
        for n in line:
            if n=='$':
                n='\$\$'
            next_states = curr_states[:]
            for m in curr_states:
                if (m, n) in self.transition_functions.keys():
                    next_states.remove(m)
                    next_states += self.transition_functions[(m, n)]
                else:
                    next_states.remove(m)
            curr_states = self.add_epsilon(next_states[:])

        for i in curr_states :
            if i in self.acceptable_states:
                vrati[1] = 1
                
        #for n in self.acceptable_states:
            #if n in curr_states:
                #acceptable = 1
        if curr_states != []:
            vrati[0] = self.id
        return vrati

def main():
    adef = open("def_automata.txt", "r")
    nizovi = open("nizovi.txt", "r")
    #a=automat("","","","")
    a.parse_adef (adef)
    for line in nizovi:
        if a.check_line(line):
            print ("OK")
        else:
            print (":(")

if __name__ == '__main__':
    main()
# vim:expandtab:

